package com.black.platform.organization.web;

import com.black.common.auth.UserContextHolder;
import com.black.common.vo.OrganizationVo;
import com.black.common.web.ApiResponse;
import com.black.common.web.ApiResponseBuilder;
import com.black.platform.codeType.vo.CodeTypeVo;
import com.black.platform.organization.po.Organization;
import com.black.platform.organization.service.handler.OrganizationHandlerService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("organization")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrganizationController {

    private final OrganizationHandlerService organizationHandlerService;

    @GetMapping("search")
    public ApiResponse<List<OrganizationVo>> getOrganizationList() {
        return ApiResponseBuilder.ok(organizationHandlerService.getOrganizationVoTreeByComCode(UserContextHolder.getMainComCode(), null, true));
    }

    @GetMapping("{comCode}")
    public ApiResponse<Organization> getByComCode(@PathVariable("comCode") String comCode) {
        return ApiResponseBuilder.ok(organizationHandlerService.getByComCode(comCode));
    }

    @GetMapping("treeVo")
    public ApiResponse<List<CodeTypeVo>> getOrganizationTreeVo() {
        return ApiResponseBuilder.ok(organizationHandlerService.getOrganizationVoTree());
    }

    @PostMapping("save")
    public ApiResponse<Boolean> save(@RequestBody Organization organization) {
        return ApiResponseBuilder.ok(organizationHandlerService.saveOrUpdate(organization));
    }

    @PostMapping("update")
    public ApiResponse<Boolean> update(@RequestBody Organization organization) {
        return ApiResponseBuilder.ok(organizationHandlerService.saveOrUpdate(organization));
    }

    @DeleteMapping()
    public ApiResponse<Boolean> delete(@RequestBody List<String> comCode) {
        return ApiResponseBuilder.ok(organizationHandlerService.delete(comCode.get(0)));
    }
    @GetMapping("/getSubComCode")
    public ApiResponse<String> getSubComCode(@RequestParam(value = "comCode") String comCode){
        return ApiResponseBuilder.ok(organizationHandlerService.getSubComCode(comCode));
    }
}
